from fastapi import APIRouter, Query, Path
from pydantic import BaseModel

router = APIRouter(prefix="/items", tags=["物品"])


# 路径参数 和 查询参数
@router.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "query": q}


# 请求体参数
class Item(BaseModel):
    name: str
    price: float
    tags: list[str] = []


@router.post("/items/")
def create_item(item: Item):
    return item


# 参数验证与文档增强描述
@router.get("/products/{product_id}")
def get_product(
    product_id: int = Path(..., title="产品 ID", ge=1),
    keyword: str = Query(None, max_length=20, description="搜索关键词"),
):
    return {"product_id": product_id, "keyword": keyword}
